/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package apteka;

import apteka.tables.Product;
import java.util.Calendar;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Marek
 */
@Component
public class ProductManager {


    Session session = null;
    
    int itemsOnPage;
    String photoUploadPath;

    public String getPhotoUploadPath() {
        return photoUploadPath;
    }

    public void setPhotoUploadPath(String photoUploadPath) {
        this.photoUploadPath = photoUploadPath;
    }

    public int getItemsOnPage() {
        return itemsOnPage;
    }

    public void setItemsOnPage(int itemsOnPage) {
        this.itemsOnPage = itemsOnPage;
    }

    public Session getSession() {
        return session;
    }

    public void setSession(Session session) {
        this.session = session;
    }

    public ProductManager() {
        session=HibernateUtil.getSession();
        this.itemsOnPage=10;
        this.photoUploadPath="../webapps/apteka_photos/";
    }

    public List<Product> getProductList(String name, int cat, int page) {
        List<Product> result = null;
        try {
            Transaction trans = session.beginTransaction();
            Query q=null;
            if (cat!=0) {
                q = session.createQuery("FROM Product AS product WHERE (product.category.idcategory="+
                        cat+" OR product.category.category.idcategory="+cat+") AND product.productname LIKE '%"+name+"%' ORDER BY product.productname");
            } else {
                q = session.createQuery("FROM Product AS product WHERE product.productname LIKE '%"+name+"%' ORDER BY product.productname");
            }
                q.setFirstResult(itemsOnPage*page);
                q.setMaxResults(itemsOnPage);
                result = (List<Product>)q.list();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return result;
    }

    public long getProductPagesCount(String name, int cat) {
        long result=0;
        try {
            Transaction trans = session.beginTransaction();
            Query q=null;
            if (cat!=0) {
                q = session.createQuery("SELECT count(*) AS ile FROM Product AS product WHERE (product.category.idcategory="+
                        cat+" OR product.category.category.idcategory="+cat+") AND product.productname LIKE '%"+name+"%'");
            } else {
                q = session.createQuery("SELECT count(*) AS ile FROM Product AS product WHERE product.productname LIKE '%"+name+"%'");
            }
            result = (Long)q.uniqueResult();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return (long)Math.ceil(result/this.getItemsOnPage())+1;
    }

    public Product getProduct(int id) {
        Product result = null;
        try {
            Transaction trans = session.beginTransaction();
            Query q = session.createQuery("FROM Product AS product WHERE product.idproduct="+id);
            result = (Product)q.uniqueResult();
        } catch (Exception e) {
            System.out.println(e.toString());
        }
        return result;
    }

    public void addProduct(Product product) {
        Transaction trans = null;
        try {
            trans = session.beginTransaction();
            trans.begin();
            session.save(product);
            session.flush();
            session.evict(product);
            trans.commit();
        } catch (Exception e) {
            System.out.println(e.toString());
            trans.rollback();
        }
    }

        public void editProduct(Product product) {
        Transaction trans = null;
        try {
            trans = session.beginTransaction();
            trans.begin();
            Product prod = getProduct(product.getIdproduct());
            prod.setProductname(product.getProductname());
            CategoryManager cat_man=new CategoryManager();
            prod.setCategory(cat_man.getCategory(product.getCategory().getIdcategory()));
            prod.setAmount(product.getAmount());
            Calendar calen = Calendar.getInstance();
            prod.setAddDate(calen.getTime());
            prod.setPrice(product.getPrice());
            prod.setDescription(product.getDescription());
            prod.setShort_desc(product.getShort_desc());
            prod.setPicturelink(product.getPicturelink());
            session.saveOrUpdate(prod);
            
            trans.commit();
        } catch (Exception e) {
            System.out.println(e.toString());
            trans.rollback();
        }
    }

        public void deleteProduct(int id) {
    
            Transaction trans=null;
            try {
                Product prod = getProduct(id);
                trans = session.beginTransaction();
                trans.begin();
                session.delete(prod);
                session.flush();
                trans.commit();
            } catch (Exception e) {
                 System.out.println(e.toString());
                 trans.rollback();
            }
        
    }

}
